package algorithm.dynamic_programming;

import java.util.Locale;

public class PublicSubsequence {

    public static int lcse(char[] str1, char[] str2){
        int[][] dp = new int[str1.length][str2.length];
        boolean temp = false;
        for (int i = 0; i < str1.length; i++) {
            if(temp)
                dp[i][0] = 1;
            if(str2[0] == str1[i] && temp != true){
                dp[i][0] = 1;
                temp = true;
            }
        }
        temp = false;
        for (int i = 0; i < str2.length; i++) {
            if(temp)
                dp[0][i] = 1;
            if(str1[0] == str2[i] && temp != true){
                dp[0][i] = 1;
                temp = true;
            }
        }

        for (int i = 1; i < str2.length; i++) {
            for (int j = 1; j < str1.length; j++) {
                if(str1[j] == str2[i])
                    dp[j][i] = dp[j - 1][i - 1] + 1;
                else
                    dp[j][i] = Math.max(dp[j - 1][i], dp[j][i - 1]);
            }
        }
        return dp[str1.length - 1][str2.length - 1];
    }

    public static void main(String[] args) {
        String str1 = "ab12f3fg";
        String str2 = "ag12335f";
        int lcse = lcse(str1.toCharArray(), str2.toCharArray());
        System.out.println(lcse);
    }
}
